<template>
  <view class="flex-col">

    <view :class="boole?'bottom115':''">
      <!--基础信息-->
      <basic-info v-model="detailForm"/>

      <!--上牌费用信息-->
      <up-card-info v-model="detailForm" :boole="boole" ref="refUpCardInfo"/>

      <!--牌照信息-->
      <license-plate v-model="detailForm" :boole="boole" ref="refLicensePlate"/>

    </view>


    <view class="button-container" v-if="boole">
      <button class="btn-item" @click="submit">提 交</button>
    </view>

  </view>
</template>

<script>
import {getRegistration, getFindCarWorkOrderInfoVo, addRegistration} from "@/api/upCard";
import basicInfo from './components/basicInfo.vue'
import upCardInfo from './components/upCardInfo.vue'
import licensePlate from './components/licensePlate.vue'

export default {
  name: 'add',
  components: {basicInfo, upCardInfo, licensePlate},
  computed: {},
  watch: {},
  data() {
    return {
      detailForm: {
        carId: "",
        workId: '',
        businessDataId: "",
        workOrderStatus: '2',
        checkCarPrice: "",
        registerPrice: "",
        photoMailPrice: "",
        isLimitCity: "",
        indexBelongCity: "",
        indexBelongCityId: "",
        indexType: "",
        payType: "",
        remark: "",
        carNo: "",
        licensePlateTime: "",
        registerUrl: "",
        driveBookUrl: "",
        sumPrice: 0,
        issuanceDate:"",
        carType:""
      },
      boole: false
    };
  },
  created() {
  },
  mounted() {
  },
  onLoad(op) {
    let title = "";

    if (op?.type === "edit") {
      title = "新增上牌工单";
      this.carBasic(op)
      this.boole = true;
    }
    if (op?.type === "view") {
      title = "上牌工单信息";
      this.boole = false;
      this.getRegistration(op)
    }
    uni.setNavigationBarTitle({
      title,
    });
  },
  methods: {
    carBasic(op) {
      console.log(op,"====")
      getFindCarWorkOrderInfoVo(op?.id).then((res) => {
        this.detailForm = res.data
        this.detailForm.carId = op?.carId
        this.detailForm.workId = op?.id
        this.detailForm.workOrderStatus = "2"
      })
    },

    async getRegistration(op) {
      try {
        const res = await getRegistration(op?.businessDataId)
        const resD = await getFindCarWorkOrderInfoVo(op?.id)
        this.detailForm = {...res.data, ...resD.data}
        this.detailForm.carId = op?.carId
        this.detailForm.workId = op?.id
        this.detailForm.workOrderStatus = "2"
      } catch (e) {
        console.error(e)
      }

    },

    async submit() {
      try {

        let arr = [
          this.$refs.refUpCardInfo.handleEffect(),
          this.$refs.refLicensePlate.handleEffect()
        ];

        // 等待所有处理完成
        await Promise.all(arr);

        // 在执行异步操作前显示加载动画
        uni.showLoading();

        // 请求注册登记信息
        let res = await addRegistration(this.detailForm);

        // 根据返回的状态码进行处理
        if (res.code === 200) {
          uni.$emit("refBackList", true);
          this.$modal.msgSuccess("提交成功");
          setTimeout(() => {
            this.$tab.navigateBack();
          }, 1000);
        }
      } catch (error) {
        if (typeof error === 'string') {
          this.$nextTick(() => {
            uni.pageScrollTo({
              selector: error,
            });
          });
        } else {
          console.error(error)
        }
      } finally {
        setTimeout(()=>{uni.hideLoading()},1000)
      }
    }
  },
};
</script>

<style scoped lang=""></style>


